// 04jyhs/shoppingMallAdmin/publishGoods/AddStandard/AddStandard.js
var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this, _data

Page({
   /*页面的初始数据 */
   data: {
      // 默认循环并显示库存
      count: [1, 1],
      // 库存输入显示和隐藏
      isCount: [1, 1]
   },
   /*生命周期函数--监听页面加载 */
   onLoad(options) {
      _this = this
      _data = this.data
      if (app.Data.skus.length != 0) {
         var moreSku = app.Data.skus
         var isCount = _data.isCount //库存是否显示
         for (var i = 0; i < moreSku.length; i++) {
            //管理进入时给库存显隐添加长度
            isCount.push(1)
            if (moreSku[i].Products_Count == -1) {
               isCount[i] = 0
            }
         }
         this.setData({
            count: moreSku,
            isCount: isCount
         })
      }
      
   },
   // switch选择无线库存
   stockShow(e) {
      var id = e.currentTarget.id
      var isCount = _data.isCount
      if (isCount[id] == 1) {
         isCount[id] = 0
      } else {
         isCount[id] = 1
      }
      this.setData({
         isCount: isCount
      })
   },
   // 删除规格
   deleteSku(e) {
      co.confirm('', '确认删除该规格吗？', '取消', '删除', function (res) {
         if (res) {
            _this.deleteThis(e)
         }
      })
   },
   deleteThis(index) {
      var count = _data.count
      var isCount = _data.isCount
      count.splice(index, 1)
      isCount.splice(index, 1)
      this.setData({
         count: count,
         isCount: isCount
      })
      app.Data.skus = count
      if (!co.empty(app.Data.skus)) { //删除规格全局暂存不为空时
         if (count.length == 1) {
            wx.navigateBack({
               delta: 1
            })
         } else if (count.length == 0) {
            app.Data.skus = ''
            wx.navigateBack({
               delta: 1
            })
         }
      }
   },
   // 添加规格
   addSku() {
      var count = _data.count
      var isCount = _data.isCount
      count.push(1)
      isCount.push(1)
      this.setData({
         count: count,
         isCount: isCount
      })
   },
   // 提交表单保存
   skuForm(e) {
      var id = e.detail.target.id //区别表单提交ID
      var skus = e.detail.value //表单提交的数据
      if (id == 1) { //添加规格时触发
         var new_sku_list = []
         var skus_List = this.getObjProducts(skus, new_sku_list)
         this.setData({
            count: skus_List
         })
         this.addSku()
      } else if (id == 2) { //保存时触发
         app.Data.skus = []
         var skus_List = this.getObjProducts(skus, app.Data.skus)
         app.Data.skus = skus_List //保存的数组暂存全局
         wx.navigateBack({
            delta: 1
         })
      } else if (id.indexOf('3') != -1) { //删除时触发
         var new_sku_list = []
         var skus_List = this.getObjProducts(skus, new_sku_list)
         this.setData({
            count: skus_List
         })
         this.deleteSku(id.split('_')[1])
      }
   },
   // 循环表单封装成对象数组
   getObjProducts(skus, new_sku_list) {
      var count = _data.count
      var isCount = _data.isCount
      // console.log("【传输的form表单】====",skus)
      for (var i = 0; i < count.length; i++) {
         var obj = {
            Sku_Name: skus['Sku_Name' + i],
            Products_PriceX: skus['Products_PriceX' + i],
            Products_Count: skus['Products_Count' + i]
         }
         if (!isCount[i]) { //管理属性时默认选择
            obj.Products_Count = -1
         }
         new_sku_list.push(obj)
      }
      return new_sku_list
   }
})